package io.renren.modules.lanling.controller;

import java.util.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.renren.common.utils.Query;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.lanling.entity.SubjectEntity;
import io.renren.modules.lanling.service.*;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.lanling.entity.StudentEntity;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import static io.renren.modules.sys.shiro.ShiroUtils.getUserId;


/**
 * 用户
 *
 * @author LANLING
 * @email lanling@gmail.com
 * @date 2018-11-08 09:36:03
 */
@RestController
@RequestMapping("lanling/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private StuSubService stuSubService;
    @Autowired
    private QuestionRecordService questionRecordService;
    @Autowired
    private QuestionErrorService questionErrorService;
    @Autowired
    private ExamDatailsService examDatailsService;
    @Autowired
    private ExamService examService;
    @Autowired
    private SearchService searchService;
    @Autowired
    private CertificateService certificateService;
    @Autowired
    private SchoolService schoolService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("lanling:student:list")
    public R list(@RequestParam Map<String, Object> params) {
        Long userId = getUserId();
        params.put("createUserId", userId);
        PageUtils page = studentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{userId}")
    @RequiresPermissions("lanling:student:info")
    public R info(@PathVariable("userId") Long userId) {
        StudentEntity student = studentService.queryObject(userId);
        return R.ok().put("student", student);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("lanling:student:save")
    public R save(@RequestBody StudentEntity student) {
        Long userId = getUserId();
        student.setCreateUserId(userId);
        //查找lanling_school中id
        int schoolId = schoolService.querySchoolId(userId);
        student.setSchoolId(schoolId);
        //保存创建者id
        student.setSchoolId(getUserId().intValue());
        //设置创建日期
        student.setCreateTime(new Date());
        //真实姓名设置为用户名
        student.setUsername(student.getRealname());
        //设置登录次数初始化次数为0
        student.setLoginCount(0);
        //密码加密
        student.setPassword(DigestUtils.sha256Hex(student.getPasswordRecord()));

        studentService.save(student);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("lanling:student:update")
    public R update(@RequestBody StudentEntity student) {
        //校验
        ValidatorUtils.validateEntity(student);

        studentService.update(student);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("lanling:student:delete")
    @Transactional(rollbackFor = Exception.class)
    public R delete(@RequestBody Long[] userIds) {

        for (Long userId : userIds) {
            //删除lanling_certificate 的关联
            certificateService.deleteByUserId(userId);

            //删除lanling_exam 的关联
            examDatailsService.deleteByUserId(userId);
            examService.deleteByUserId(userId);

            //删除lanling_question_error 的关联
            questionErrorService.deleteByUserId(userId);

            //删除lanling_question_record 的关联
            questionRecordService.deleteByUserId(userId);

            //删除lanling_search的关联
            searchService.deleteByUserId(userId);

            //删除lanling_stu_sub 的关联
            stuSubService.deleteByUserId(userId);

            //删除lanling_token的关联
            tokenService.deleteByUserId(userId);
        }

        studentService.deleteBatchIds(Arrays.asList(userIds));

        return R.ok();
    }


    @RequestMapping("/queryList")
    public R queryList() {
        Long userId = getUserId();
        Map<String, Object> params = new HashMap<>();
        params.put("createUserId", userId);
        Query query = new Query(params);
        List<StudentEntity> studentEntities = studentService.queryList(query);
        return R.ok().put("list", studentEntities);
    }

}
